package ro.imc.andrei.visual;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import ro.imc.andrei.beans.Course;
import ro.imc.andrei.beans.Group;
import ro.imc.andrei.beans.Professor;
import ro.imc.andrei.beans.Student;
import ro.imc.andrei.ums.UMSDB;

public class CustomTableModel extends AbstractTableModel{

	private static final long serialVersionUID = 741881415769497883L;
	private int type;
	private List<Professor> professorList = new ArrayList<Professor>();
	private List<Student> studentList = new ArrayList<Student>();
	private List<Group> groupList = new ArrayList<Group>();
	private List<Course> courseList = new ArrayList<Course>();
	
	String[] studentColumns = {"First Name", "Last Name", "CNP", "Student Number", "Status", "Faculty"};
	String[] professorColumns = {"First Name", "Last Name", "CNP", "Title", "Field"};
	String[] courseColumns = {"Course ID", "Course Name", "Mandatory", "Semester", "Year of Study"};
	
	@Override
	public int getColumnCount() {
		switch(type){
			case 0://Student
				return 6;
			
			case 1://Profesor
				return 5;
			
			case 2://Groupes
				return 4;
				
			case 3://Course
				return 5;
				
			default: 
				return 0;
		}
	}
	
	@Override
	public String getColumnName(int col){
		switch(type){
			case 0: return studentColumns[col];
			case 1: return professorColumns[col];
			//Group case
			case 3: return courseColumns[col];
		}
		
		return null;
	}

	@Override
	public int getRowCount() {
		switch(type){
			case 0: return studentList.size();
			case 1: return professorList.size();
			//Group case
			case 3: return courseList.size();
		}
		return 0;
	}

	@Override
	public Object getValueAt(int row, int col) {
		
		switch(type){
			case 0:{ //student
				switch(col){
					case 0: return studentList.get(row).getFirstName();
					case 1: return studentList.get(row).getLastName();
					case 2: return studentList.get(row).getCNP();
					case 3: return studentList.get(row).getStudentNo();
					case 4: return studentList.get(row).getStatus();
					case 5: {
						try{
							Class.forName("com.mysql.jdbc.Driver");
							Connection con = DriverManager.getConnection(UMSDB.getInstance().DATABASE_URL);
							Statement stmt = con.createStatement();
							ResultSet rs = stmt.executeQuery("SELECT facultyName FROM faculty WHERE facultyId = '" + studentList.get(row).getFaculty() + "'");
							
							if(rs.next())
								return rs.getString("facultyName");
							return "Neinscris";
						}
						catch(Exception e){
							e.printStackTrace();
						}
					}
				}
			}
			
			case 1:{//Professor
				switch(col){
					case 0: return professorList.get(row).getFirstName();
					case 1: return professorList.get(row).getLastName();
					case 2: return professorList.get(row).getCNP();
					case 3: return professorList.get(row).getTitle();
					case 4: return professorList.get(row).getField();
				}
			}
			
			case 2:{//Group
				switch(col){
					case 0: return groupList.get(row).getGroupName();
					case 1: return groupList.get(row).getYearOfStudy();
					case 2: return groupList.get(row).getFieldOfStudy();
				}
			}
			
			case 3:{//Course
				switch(col){
					case 0: return courseList.get(row).getCourseId();
					case 1: return courseList.get(row).getCourseName();
					case 2: return courseList.get(row).isMandatory();
					case 3: return courseList.get(row).getSemester();
					case 4: return courseList.get(row).getYearOfStudy();
				}
			}
			
			default: return null;
		}
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	//Method for managing student search
	public void addRow(Student student) {
		studentList.add(student);
	}

	//Method for managing professor search
	public void addRow(Professor prof) {
		professorList.add(prof);		
	}

	//Method for managing course search
	public void addRow(Course course) {
		courseList.add(course);
	}

}
